<?php
namespace app\index\controller;

use think\Db;
use think\Session;
use think\Controller;

class User extends Controller
{
    //用户首页内容显示
    public function yonghu(){
        //sql语句，定义为全局变量
        $arr=db('user')
            ->join('f_message','f_message.uid=f_user.uid','left')
            ->join('f_real','f_user.uid=f_real.uid','left')
            ->join('f_fans','f_fans.uid=f_user.uid','left')
            ->order('f_user.uid desc')
            ->where('f_user.phone','neq','')
            ->field(['f_user.uid','f_user.code','f_user.avatars_img','f_user.head_portrait','f_user.phone','f_user.uname','f_real.rflg','f_user.company','f_user.nickname','f_user.uflg','count(f_message.uid)' => 'xinxi','f_fans.fuid','f_fans.guid','f_real.starttime','f_real.stoptime'])
            ->group('f_user.uid');
        //判断是否点击和显示内容
        if(input('sub'))
        {
            $cateid=input('cateid');//下拉菜单
            $key=input('Key');//输入框
            $datemin = input('datemin', '');//最小时间
            $datemax = input('datemax', '');//最大的吧
            //判断时间
            if (isset($datemin) && $datemin != '') {
                $arr = $arr->whereTime('f_user.createtime', '>=', input('datemin') . ' 00:00:00');
            }
            if (isset($datemax) && $datemax != '') {
                $arr = $arr->whereTime('f_user.createtime', '<=', input('datemax') . ' 23:59:59');
            }
            if($cateid=='')
            {
                $arr=$arr->paginate(9,false,['query'=>request()->param()]);
                $page=$arr->render();
                return view('yonghu',['arr'=>$arr,'page'=>$page]);
            }
            if($key==''){
                $arr=$arr->paginate(9);
                $page=$arr->render();
                return view('yonghu',['arr'=>$arr,'page'=>$page]);
            }else {
                if($cateid=='uname'){
                    $arr = $arr->where('uname|nickname', 'like', '%' . $key . '%')->paginate(9);
                    $page = $arr->render();
                    return view('yonghu', ['arr' => $arr, 'page' => $page]);
                }else {
                    $arr = $arr->where($cateid, 'like', '%' . $key . '%')->paginate(9);
                    $page = $arr->render();
                    return view('yonghu', ['arr' => $arr, 'page' => $page]);
                }
            }
        }else{
            $arr=$arr->paginate(9);
            $page=$arr->render();
            return view('yonghu',['arr'=>$arr,'page'=>$page]);
        }

    }


    //查看详情页
    public function chakan(){
        $uid=input('uid');
        //个人信息查询
        $arr=db('user')
            ->join('f_message','f_message.uid=f_user.uid','left')//与信息表相连
            ->join('f_fans','f_fans.uid=f_user.uid','left')//与关注粉丝表向连
            ->join('f_company','f_company.uid=f_user.uid','left')//与企业表想连
            ->join('f_real','f_real.uid=f_user.uid','left')//与实名认证表想连
            ->join('f_package','f_package.tid=f_real.tid','left')//套餐表与实名认证表
            ->where('f_user.uid',$uid)
            ->field('f_user.*,count(f_message.uid) as s,f_fans.fuid,f_fans.guid,f_company.*,f_real.rflg,f_real.s_createtime,f_real.card,f_real.front_img,f_real.reverse_img,f_real.uid,f_real.weixin,f_package.*')//需要查询的表条件
            ->group('f_user.uid')
            ->select();
        //dump($arr);die;
        //公司能力显示
        foreach ($arr as $key) {
            $k=$key['aid'];
            $ke=$key;
        }
        
        if($k==''){
            $nengli=[0=>['ability'=>'']];
            $this->assign('nengli',$nengli);
        }
        //dump($k);die;
        $place_img=array_filter(explode(',',$ke['place_img']));//办公场所图片
        $location_img=array_filter(explode(',',$ke['location_img']));//厂房外景图片
        $equipment_img=array_filter(explode(',',$ke['equipment_img']));//常规设备图片
        $license_img=array_filter(explode(',',$ke['license_img']));//营业执照
        $aid=array_filter(explode(',',$k));//分割
        foreach ($aid as $aids) {
            $nengli[]=db('ability')->where('aid',$aids)->field('ability')->find();//查询
            $this->assign('nengli',$nengli);//映射
        }

        //信息流表查询
        $xinxi=db('message')
            ->join('f_user','f_user.uid=f_message.uid')
            ->where('f_user.uid',$uid)
            ->select();
        //粉丝关注
        $fanss=db('fans')->where('uid',$uid)->select();
        if($fanss) {
            foreach ($fanss as $key) {
                $f1 = $key['fuid'];
                $g1 = $key['guid'];
            }
            //粉丝
            if ($f1 == '') {
                $f6 = [];
                $this->assign('f6', $f6);
            } else {
                $f2 = array_filter(explode(',', $f1));
                foreach ($f2 as $f21) {
                    //dump($uid);
                   $f4 = db('user')
                       ->join('f_ribiao', 'f_ribiao.uid=f_user.uid')
                       ->where('bid',$uid)
                       ->where('f_user.uid', $f21)
                       ->field('f_user.code,f_user.uid,f_user.uname,f_user.nickname,f_user.avatars_img,f_user.head_portrait,f_ribiao.createtime')
                       ->select();
                    $f3[] = $f4;
                }

                foreach ($f3 as $f4) {
                    foreach ($f4 as $f5) {
                        $f6[] = $f5;
                    }
                }
                //dump($f3);
                //dump($f6);die;
                $this->assign('f6', $f6);
            }
            //关注
            if ($g1 == '') {
                $g6 = [];
                $this->assign('g6', $g6);
            } else {
                $g2 = array_filter(explode(',', $g1));
                foreach ($g2 as $g21) {
                    $g4 = db('ribiao')
                        ->join('f_user','f_user.uid=f_ribiao.bid','left')
                        ->where('bid',$g21)
                        ->where('f_ribiao.uid',$uid)
                        ->field('f_user.uid,f_user.uname,f_user.nickname,f_user.avatars_img,f_user.head_portrait,f_ribiao.createtime')
                        ->select();
                    $g3[] =$g4;
                }
                foreach ($g3 as $g4) {
                    foreach ($g4 as $g5) {
                        $g6[]=$g5;
                    }
                }
                $this->assign('g6', $g6);
            }
        }else{
            $f6 = [];
            $this->assign('f6', $f6);
            $g6 = [];
            $this->assign('g6', $g6);
        }
        return view('chakan',[
            'arr'=>$arr,
            'xinxi'=>$xinxi,
            'place_img'=>$place_img,//办公场所图片
            'location_img'=>$location_img,//厂房外景图片
            'equipment_img'=>$equipment_img,//常规设备图片
            'license_img'=>$license_img,//营业执照

        ]);
    }

    //锁定控制器
    public function suoding()
    {
        $uid=input('uid');
        $cha=db('user')->where('uid',$uid)->field('f_user.uflg,f_user.gflg')->find();
        if($cha['gflg']==1){
            return 2;
        }else {
            if ($cha['uflg'] == 1) {
                $uflg = 0;
            } else {
                $uflg = 1;
            }
            $arr = db('user')
                ->where('f_user.uid', $uid)
                ->update([
                    'uflg' => $uflg
                ]);
            if ($arr) {
                return 1;
            } else {
                return 0;
            }
        }
    }
    /*
     * 查看详情修改资料
     */
    public function xiugai()
    {
        $uid=input('uid');
        if(request()->isAjax()){
            //echo input('location_imgs');die;
            $location_img=input('location_img').input('location_imgs');//厂房背景图片
            $place_img=input('place_img').input('place_imgs');//厂房背景图片
            $equipment_img=input('equipment_img').input('equipment_imgs');//厂房背景图片
            $license_img=input('license_img').input('license_imgs');//厂房背景图片
            $product=input('product');//主营产品
            if(input('post.name/a')==''){
                $nengli='';
            }else{
                $nengli=implode(',',input('post.name/a')).',';
            }//公司能力
            $information=input('information');//基本信息
            $ability=input('ability');//交付能力
            $introduce=input('introduce');//公司介绍
            //修改
            $box=db('company')
                ->where('uid',$uid)
                ->update([
                    'product'=>$product,
                    'information'=>$information,
                    'ability'=>$ability,
                    'introduce'=>$introduce,
                    'aid'=>$nengli,
                    'location_img'=>$location_img,
                    'license_img'=>$license_img,
                    'equipment_img'=>$equipment_img,
                    'place_img'=>$place_img,
                ]);
            if($box===0) {
                return 1;
            }elseif($box){
                return 1;
            }else{
                return 0;
            }
        }else{
            $arr=db('company')->where('uid',$uid)->find();//公司查询
            $aid=array_filter(explode(',',$arr['aid']));//
            $ability=db('ability')->select();//能力查询
            $location_img=array_filter(explode(',',$arr['location_img']));//厂房
            $place_img=array_filter(explode(',',$arr['place_img']));//办公
            $this->assign('place_img',$place_img);
            $license_img=array_filter(explode(',',$arr['license_img']));//营业
            $this->assign('license_img',$license_img);
            $equipment_img=array_filter(explode(',',$arr['equipment_img']));//设备
            $this->assign('equipment_img',$equipment_img);
            return view('xiugai',['aid'=>$aid,'arr'=>$arr,'location_img'=>$location_img,'ability'=>$ability]);
        }
    }
    //图片上传
    public function doUpload()
    {
        $id=input('uid');
        $file = request()->file('file');
        //命名，避免高并发
        $fileName=md5(uniqid(md5(microtime(true)),true));
        //图片路径
        $imgUrl= ROOT_PATH . '/upload/news/';
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move($imgUrl,$fileName);
        $error = $file->getError();
        if ($info) {
            // 成功上传后 获取上传信息
            $url= $info->getSaveName().',';
            return json(['code' => 1, 'msg' => '上传成功!', 'url' =>$url]);
        } else {
            // 上传失败获取错误信息
            return json(['code' => 0, 'msg' => $file->getError(), 'url' => '']);
        }
    }
    //厂房外景图片控制器
    public function location_img()
    {
        $uid=input('uid');//获取用户id
        $urlimg=input('urlimg');//获取图片的下标
        $rs=db('company')->where('uid',$uid)->find();
        //array_filter:去空函数
        $arr=array_filter(explode(',',$rs['location_img']));//以,拆分营业执照字段
        //foreach循环得到$arr字段内的下标并与$urlimg获取的下标进行对比然后删除数据库数据
        foreach ($arr as $key=>$val)
        {
            if($key==$urlimg){
                $path=ROOT_PATH . '/upload/news/'.$val;//得到要删除的图片地址
                @unlink($path);   //删除图片
                clearstatcache();//清缓存
            }
            unset($arr[$urlimg]);//删除数据库数据
        }
        $str = implode(",",$arr);
        if($str==''){
            $shan=db('company')->where('uid',$uid)->update(['location_img'=>'']);
        }else{
            $shan=db('company')->where('uid',$uid)->update(['location_img'=>$str.","]);
        }
        if ($shan)
        {
            return 1;
        }else {
            return 2;
        }
    }
//常规设备照片控制器
    public function equipment_img()
    {
        $uid=input('uid');//获取用户id
        $urlimg=input('urlimg');//获取图片的下标
        $rs=db('company')->where('uid',$uid)->find();
        //array_filter:去空函数
        $arr=array_filter(explode(',',$rs['equipment_img']));//以,拆分营业执照字段
        //foreach循环得到$arr字段内的下标并与$urlimg获取的下标进行对比然后删除数据库数据
        foreach ($arr as $key=>$val)
        {
            if($key==$urlimg){
                $path=ROOT_PATH . '/upload/news/'.$val;//得到要删除的图片地址
                @unlink($path);   //删除图片
                clearstatcache();//清缓存
            }
            unset($arr[$urlimg]);//删除数据库数据
        }
        $str = implode(",",$arr);
        if($str==''){
            $shan=db('company')->where('uid',$uid)->update(['equipment_img'=>'']);
        }else{
            $shan=db('company')->where('uid',$uid)->update(['equipment_img'=>$str.","]);
        }
        if ($shan)
        {
            return 1;
        }else {
            return 2;
        }
    }
    //办公设备照片控制器
    public function place_img()
    {
        $uid=input('uid');//获取用户id
        $urlimg=input('urlimg');//获取图片的下标
        $rs=db('company')->where('uid',$uid)->find();
        //array_filter:去空函数
        $arr=array_filter(explode(',',$rs['place_img']));//以,拆分营业执照字段
        //foreach循环得到$arr字段内的下标并与$urlimg获取的下标进行对比然后删除数据库数据
        foreach ($arr as $key=>$val)
        {
            if($key==$urlimg){
                $path=ROOT_PATH . '/upload/news/'.$val;//得到要删除的图片地址
                @unlink($path);   //删除图片
                clearstatcache();//清缓存
            }
            unset($arr[$urlimg]);//删除数据库数据
        }
        $str = implode(",",$arr);
        if($str==''){
            $shan=db('company')->where('uid',$uid)->update(['place_img'=>'']);
        }else{
            $shan=db('company')->where('uid',$uid)->update(['place_img'=>$str.","]);
        }
        if ($shan)
        {
            return 1;
        }else {
            return 2;
        }
    }
    //营业执照设备照片控制器
    public function license_img()
    {
        $uid=input('uid');//获取用户id
        $urlimg=input('urlimg');//获取图片的下标
        $rs=db('company')->where('uid',$uid)->find();
        //array_filter:去空函数
        $arr=array_filter(explode(',',$rs['license_img']));//以,拆分营业执照字段
        //foreach循环得到$arr字段内的下标并与$urlimg获取的下标进行对比然后删除数据库数据
        foreach ($arr as $key=>$val)
        {
            if($key==$urlimg){
                $path=ROOT_PATH . '/upload/news/'.$val;//得到要删除的图片地址
                @unlink($path);   //删除图片
                clearstatcache();//清缓存
            }
            unset($arr[$urlimg]);//删除数据库数据
        }
        $str = implode(",",$arr);
        if($str==''){
            $shan=db('company')->where('uid',$uid)->update(['license_img'=>'']);
        }else{
            $shan=db('company')->where('uid',$uid)->update(['license_img'=>$str.","]);
        }
        if ($shan)
        {
            return 1;
        }else {
            return 2;
        }
    }
}